var firstClassType;
var id;
var groupid;
var iconid;
var subid;
var menuid;

$(function(){
   	 groupid = ly.getUrlParam("groupid");
	 iconid = ly.getUrlParam("iconid");
	 subid = ly.getUrlParam("subid");
	 menuid = ly.getUrlParam("menuid");	

	firstClassType = ly.getUrlParam("globalSystemParamType");
	id = ly.getUrlParam("id");
	if(firstClassType == 2){
		$("#title").html("编辑");
		  $(".form").getForm({
	        id: id
	   },function (data) {
	    	if(data.paramCode =='isOrderSendMerchandiseCoupons'){
	    		//下单送商品优惠券
	    		grantCoupon("1",data.conpouId);
	    		if(data.paramValue=='0'){
	    			$("#addGrant").removeClass("hide");
	    			$("#addConditionParam").removeClass("hide");
		    	}else{
		    		$("#addGrant").addClass("hide");
		    		$("#addConditionParam").addClass("hide");
		    	}
	    	}else if(data.paramCode =="isOrderTicketCoupon"){
	    		//下单送门票优惠券
	    		grantCoupon("4",data.conpouId);
	    		if(data.paramValue=='0'){
    				$("#addGrant").removeClass("hide");
    				$("#addConditionParam").removeClass("hide");
		    	}else{
		    		$("#addGrant").addClass("hide");
		    		$("#addConditionParam").addClass("hide");
		    	}
	    	}else if(data.paramCode =='isOrderMealCoupons'){
	    		//下单送点餐优惠券
	    		grantCoupon("2",data.conpouId);
	    		if(data.paramValue=='0'){
	    			$("#addGrant").removeClass("hide");
	    			$("#addConditionParam").removeClass("hide");
		    	}else{
		    		$("#addGrant").addClass("hide");
		    		$("#addConditionParam").addClass("hide");
		    	}
	    	}
	    });
	}	
	
	
	//提交
    $("body").on("click","#save-btn-commodityClassification",function(event){
        event.preventDefault();
        var success =  $('.form').validate('submitValidate');
        if(success){
            //验证通过提交表单
            submitFom();
        }
    })
  
    $("body").on("change","#paramValue",function(){
    	var paramCode = $("#paramCode").val();
    	if(paramCode == 'isOrderSendMerchandiseCoupons' || paramCode== 'isOrderTicketCoupon' || paramCode == 'isOrderMealCoupons'){
    		var paramValue = $(this).val();
    		if(paramValue==0){
    			$("#addGrant").removeClass("hide");
    			$("#addConditionParam").removeClass("hide");
	    	}else{
	    		$("#addGrant").addClass("hide");
	    		$("#addConditionParam").addClass("hide");
	    	}
    	}
    });
});

/*
 * 提交表单
 */
function submitFom(){
    var paramName =$("#paramName").val();
 	var paramValue =$("#paramValue").val();
 	var paramDesc = $("#paramDesc").val();
 	var conditionParam = $("#conditionParam").val();
  	var obj = {};
  	var url="";
  	var conpouId = "";
	$("input[name=grant]:checked").each(function(){
		conpouId += $(this).val()+',';
	});
  	
  	if(firstClassType == 1){
		//新增
		url = site.apiUrl+"/back/globalSystemParam/insertGlobalSystemParam";
		 obj = {
	    	'paramName':paramName,
	    	'paramValue':paramValue,
	    	'paramDesc':paramDesc,
	    	'conpouId':conpouId,
	    	'conditionParam':conditionParam
	    };
  	}else{
  		//修改
  		var thisId = $("#thisId").val();
  		url = site.apiUrl+"/back/globalSystemParam/updateGlobalSystemParam";
  		obj = {
  			'paramName':paramName,
	    	'paramValue':paramValue,
	    	'paramDesc':paramDesc,
	    	'conpouId':conpouId,
	    	'conditionParam':conditionParam,
  			'id':thisId
  		};
  	}
  	console.log(obj);
	request({
		url:url,
		data:obj,
		type:"post",
		ajaxform:'true',
		callback:function(f) {
			console.log(f);
			if(f.code=="rest.success"){
				if(firstClassType == 1){
					ly.alert('保存成功',function(){	            
			    		ly.goto("globalSystemParam.html");
			        });
				}else{
					ly.alert('修改成功',function(){	           
			    		ly.goto("globalSystemParam.html");
			        });
				}
			}else{
				ly.toast(f.desc);
			}
		}
	});
  	
}
function grantCoupon(couponType,grantId){
	var list = new Array();
	request({
		url:site.interUrl+"/coupon/selectCoupon",
		data:{'num':1,"size":100,"couponType":couponType,"isValidity":1},
		type:"get",
		//ajaxform:'true',
		callback:function(f) {
			console.log(f);
			if(f.code=="rest.success"){
				var obj = {};
				$.each(f.result.list,function(i,o){
					obj= {value:o.id,display:o.couponName};
					list.push(obj);
				}) 
				
				console.log(list);
				if(list.length >0){
					$("#grant").getSelect({
						type:"checkbox",
						asName:"grant",
						list:list,
						default:grantId	
					});	
				}else{
					$("#grant").html("暂无优惠券，去  <a href = 'coupon.html?groupid=18c64b27d3344d2593cf2d548b354812'>添加</a>");
				}
				
				$("#addConditionParam").removeClass("hide");
			}else{
				ly.toast(f.desc);
				return false;
			}
		}
	});
	
}